//
//  ZJTabBarController.m
//  报销封装
//
//  Created by ZJ on 17/3/20.
//
//

#import "ZJTabBarController.h"
#import "UIColor+additions.h"
#import "MyViewController.h"
#import "ElectiveViewController.h"
#import "ZJSubjectClassViewController.h"
#import "HomeMenuViewController.h"
#import "YBElectiveViewController.h"
@interface ZJTabBarController ()
{
    NSInteger _seleTabBarIndex;
}
@end

@implementation ZJTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
     NSLog(@"itab = %lf",self.tabBar.bounds.size.height);
    _seleTabBarIndex = 0;
    HomeMenuViewController *menuVc = [[HomeMenuViewController alloc] init];
    [self addChildVc:menuVc title:@"首页" image:@"tab_index_n" selectedImage:@"tab_index_s"];
    
    YBElectiveViewController *homeVc = [[YBElectiveViewController alloc] init];
    
    [self addChildVc:homeVc title:@"选课" image:@"tab_ele_n" selectedImage:@"tab_ele_s"];

    ZJSubjectClassViewController *addressBookVc = [[ZJSubjectClassViewController alloc] init];
    [self addChildVc:addressBookVc title:@"专题班" image:@"tab_le_n" selectedImage:@"tab_learn_s"];

    MyViewController *myVC = [[MyViewController alloc] init];
    [self addChildVc:myVC title:@"我的" image:@"tab_my_n" selectedImage:@"tab_my_s"];
 
    CAGradientLayer *layer = [CAGradientLayer new];
    layer.colors = @[(__bridge id)[UIColor zj_colorWithHexString:@"f9d5c9"].CGColor, (__bridge id)[UIColor zj_colorWithHexString:@"feb69e"].CGColor];
    layer.startPoint = CGPointMake(0, 0);
    layer.endPoint = CGPointMake(0, 1);
    layer.frame = self.tabBar.bounds;
    NSMutableArray * tempArray = [self.tabBar.layer.sublayers mutableCopy];
    [tempArray insertObject:layer atIndex:0];
    self.tabBar.layer.sublayers = [tempArray copy];
    
}






- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    childVc.title = title;
    childVc.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = [UIColor zj_colorWithHexString:@"b7babf"];
    NSMutableDictionary *selectTextAttrs = [NSMutableDictionary dictionary];
    selectTextAttrs[NSForegroundColorAttributeName] = [UIColor themeColor];
    [childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    [childVc.tabBarItem setTitleTextAttributes:selectTextAttrs forState:UIControlStateSelected];
    [self addChildViewController:childVc];
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    NSInteger index = [tabBar.items indexOfObject:item];
    if (index == _seleTabBarIndex) {
        [[NSNotificationCenter defaultCenter] postNotificationName:NOTI_UPDATE_TAB object:@{@"index":[NSString stringWithFormat:@"%ld",_seleTabBarIndex]}];
    }else{
        _seleTabBarIndex = index;
    }
}


@end
